#!/usr/bin/python3
# -*- coding: utf-8 -*-
import wx
import conf
import db
import wx.grid
import base64


# 需要安装 wxPython
class MyApp(wx.Frame):
    def __init__(self, parent, title):
        super(MyApp, self).__init__(parent, title=title, size=(1024, 480))
        panel = wx.Panel(self, size=(1024, 480))
        vbox = wx.BoxSizer(wx.VERTICAL)

        # name
        boxHead = wx.BoxSizer(wx.HORIZONTAL)
        # button
        self.btncipher = wx.Button(panel, -1, "根据TID查TRACE", size=(80, 22))
        boxHead.Add(self.btncipher, 1, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)
        self.btncipher.Bind(wx.EVT_BUTTON, self.OnEnterCipher)
        # text
        self.areahead = wx.TextCtrl(panel, -1, "e1f64965-40f9-4a8b-a839-c885076e698a", size=(300, 22))
        boxHead.Add(self.areahead, 2, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)

        self.host = wx.TextCtrl(panel, -1, "52.83.105.149", size=(120, 22))
        boxHead.Add(self.host, 3, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)

        self.port = wx.TextCtrl(panel, -1, "1445", size=(30, 22))
        boxHead.Add(self.port, 4, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)

        self.name = wx.TextCtrl(panel, -1, "SMDCA", size=(50, 22))
        boxHead.Add(self.name, 5, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)

        self.uname = wx.TextCtrl(panel, -1, "mdcauser", size=(50, 22))
        boxHead.Add(self.uname, 6, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)

        self.upwd = wx.TextCtrl(panel, -1, "Mysql!123456", size=(50, 22))
        boxHead.Add(self.upwd, 7, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)
        
        self.bt = wx.Button(panel, -1, "连接", size=(50, 22))
        boxHead.Add(self.bt, 8, wx.ALIGN_LEFT | wx.ALL | wx.EXPAND, 5)
        self.bt.Bind(wx.EVT_BUTTON, self.OnEnterConnect)
        
        vbox.Add(boxHead)

        # cipher
        boxCipher = wx.BoxSizer(wx.HORIZONTAL)
        self.show = wx.grid.Grid(panel, -1)
        self.show.CreateGrid(20, 4)
        self.show.SetColLabelValue(0, "REMARK")
        self.show.SetColLabelValue(1, "SID")
        self.show.SetColLabelValue(2, "CTIME")
        self.show.SetColLabelValue(3, "ID")

        self.show.SetColSize(0, 400)
        self.show.SetColSize(1, 150)
        self.show.SetColSize(2, 200)
        self.show.SetColSize(3, 150)

        boxCipher.Add(self.show, 11,  wx.ALL | wx.ALIGN_LEFT | wx.EXPAND, 5)
        vbox.Add(boxCipher)

        panel.SetSizer(vbox)

        self.Centre()
        self.Show()
        self.Fit()

    def OnEnterConnect(self, event):
        conf.db_name = self.name.GetValue()
        conf.db_host = self.host.GetValue()
        conf.db_port = int(self.port.GetValue(), 10)
        conf.db_user_pwd = self.upwd.GetValue()
        conf.db_user_name = self.uname.GetValue()
        mesg = ""
        if not conf.db_name:
            mesg += "db_name is null,"
        if not conf.db_host:
            mesg += "db_host is null,"
        if not conf.db_port:
            mesg += "db_port is null,"
        if not conf.db_user_pwd:
            mesg += "db_user_pwd is null,"
        if not conf.db_user_name:
            mesg += "db_user_name is null,"
        if mesg:
            box = wx.MessageDialog(None, mesg, 'Message', wx.OK)
            box.ShowModal()
            box.Destroy()

        db.release_conn()
        try:
            db.create_conn()
        except ... as e:
            box = wx.MessageDialog(None, '链接失败！', 'Message', wx.OK)
            box.ShowModal()
            box.Destroy()
        return True

    def OnEnterCipher(self, event):
        self.show.ClearGrid()
        tid = self.areahead.GetValue()
        if not tid:
            return
        ret = db.execute_fetchall("SELECT ID, TID, SID, REMARK, CTIME FROM LTR_TASK_TRACE WHERE TID = '{}' ORDER BY CTIME DESC".format(tid))
        line = 0
        maxLine = len(ret)
        rows = self.show.GetNumberRows()
        if rows < maxLine:
            self.show.AppendRows(maxLine-rows)

        for li in ret:
            s = li[3]
            if s[:9] == 'base64://':
                s = s[9:]
                s = base64.b64decode(s)
                s = s.decode()
            self.show.SetCellValue(line, 0, s)

            self.show.SetCellValue(line, 1, li[2])
            self.show.SetCellValue(line, 2, str(li[4]))
            self.show.SetCellValue(line, 3, li[0])
            line += 1
        self.show.ForceRefresh()


if __name__ == "__main__":
    app = wx.App()
    MyApp(None, 'TID')
    app.MainLoop()
